package com.dbpediamobile;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

/*
 * Autor: Camilo Verdugo.
 * Metodo utilizado para gestionar las consultas a la BD de Bookmarks de mapa.
 * Crea la tabla en caso de no existir y obtiene e inserta datos
 */

public class MapaBD {

		public static final  String 	KEY_ROWID 		= "id";
	 	public static final  String 	KEY_LAT  		= "latitud";
	 	public static final  String 	KEY_LON  		= "longitud";
	 	public static final  String 	KEY_NAME 		= "nombre";
	    private static final String DATABASE_NAME 		= "db_coordenadas";
	    private static final String DATABASE_TABLE 		= "coordenada";
	    private static final int 	DATABASE_VERSION 	= 1;
	    private static final String TAG = "DBAdapter";
	    
	    private static final String DATABASE_CREATE =
	    	"CREATE TABLE "+DATABASE_TABLE+" ("+KEY_ROWID +" integer primary key autoincrement, "
	        +KEY_LAT +" text not null, "+KEY_LON +" text not null, " + KEY_NAME +" text not null);";	        
	    private final Context context; 	    
	    private DatabaseHelper DBHelper;
	    private SQLiteDatabase db;
	    private SQLiteStatement insertStmt;
	    private static final String INSERT = "INSERT INTO "+DATABASE_TABLE+"("+KEY_LAT+","+KEY_LON+","+KEY_NAME+") VALUES (?,?,?)";

	    public MapaBD(Context ctx) 
	    {
	        this.context = ctx;
	        DBHelper = new DatabaseHelper(context);
	        this.db = DBHelper.getWritableDatabase();
	        this.insertStmt = this.db.compileStatement(INSERT);	        
	    }
	    
	    public long insert(BookmarksItemMap b) {
	    	this.insertStmt.bindString(1, b.lat);
	    	this.insertStmt.bindString(2, b.lon);
	    	this.insertStmt.bindString(3, b.name); 
	    	return this.insertStmt.executeInsert();
	    }
	    
	    public int deleteAll(){
	    	return db.delete(DATABASE_TABLE ,null,null);
	    }
	    
	    public ArrayList<BookmarksItemMap> selectAll() {
	    	ArrayList<BookmarksItemMap> list = new ArrayList<BookmarksItemMap>();
	    	Cursor cursor = this.db.query(DATABASE_TABLE, new String[] { KEY_NAME , KEY_LAT,KEY_LON}, null, null, null, null, null);
	    	if (cursor.moveToFirst()) {
	    		do {
	    			BookmarksItemMap b = new BookmarksItemMap();
	    	        b.name = cursor.getString(0);
	    	        b.lat = cursor.getString(1);
	    	        b.lon = cursor.getString(2);
	    	        list.add(b);
	    	    } while (cursor.moveToNext());
	    	}
	    	if (cursor != null && !cursor.isClosed()) {
	    		cursor.close();
	    	}
	    	return list;
	   }
	    
	   

	    public boolean updateTitle(long rowId, BookmarksItemMap b) {
	        ContentValues args = new ContentValues();
	        args.put(KEY_LAT, b.lat);
	        args.put(KEY_LON, b.lon);
	        args.put(KEY_NAME, b.name);
	        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	    }
	
	
	    private static class DatabaseHelper extends SQLiteOpenHelper {
		       DatabaseHelper(Context context) {
		           super(context, DATABASE_NAME, null, DATABASE_VERSION);
		       }

			   @Override
			   public void onCreate(SQLiteDatabase db) {
				   db.execSQL(DATABASE_CREATE);
			   }
		
			   @Override
			   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
				   Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
			       db.execSQL("DROP TABLE IF EXISTS titles");
			       onCreate(db);
			   }
	    }    
}
